package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.equipment.entity.EquipmentArea;
import com.mxpio.erp.common.equipment.entity.EquipmentCheckPlan;
import com.mxpio.erp.common.equipment.entity.EquipmentCheckPlanTarget;
import com.mxpio.erp.common.equipment.entity.EquipmentEqpBasics;
import com.mxpio.erp.common.equipment.service.EquipmentAreaService;
import com.mxpio.erp.common.equipment.service.EquipmentCheckPlanService;
import com.mxpio.erp.common.equipment.service.EquipmentCheckPlanTargetService;
import com.mxpio.erp.common.equipment.service.EquipmentEqpRelCategoryService;
import com.mxpioframework.common.exception.MBootException;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 点巡检计划目标
 * @Author: jeecg-boot
 * @Date: 2021-09-16
 * @Version: V1.0
 */
@Service
@Slf4j
public class EquipmentCheckPlanTargetServiceImpl extends BaseServiceImpl<EquipmentCheckPlanTarget> implements EquipmentCheckPlanTargetService {


    @Autowired
    private EquipmentCheckPlanService equipmentCheckPlanService;
    @Autowired
    private EquipmentEqpRelCategoryService equipmentEqpRelCategoryService;
    @Autowired
    private EquipmentAreaService equipmentAreaService;

    @Override
    @Transactional
    public List<EquipmentCheckPlanTarget> selectByMainId(String mainId) {
        Criteria c = Criteria.create().addCriterion("planId", Operator.EQ, mainId);
        return this.list(EquipmentCheckPlanTarget.class, c);
    }

    @Override
    @Transactional
    public void addTargetByCatgoryId(String planId, String categoryId) {
        // 查询plan
        EquipmentCheckPlan plan = equipmentCheckPlanService.getById(EquipmentCheckPlan.class, planId);
        if (plan == null || plan.getIsEnable() == 0) {
            return;
        }

         //查询出来分类以及其子分类下的所有设备
        List<EquipmentEqpBasics> equipmentEqpBasics = equipmentEqpRelCategoryService.listAllEqpByCategoryId(categoryId);

        if (CollectionUtils.isEmpty(equipmentEqpBasics)) {
            return;
        }
        equipmentEqpBasics.forEach(eqp -> {
            Long count = JpaUtil.linq(EquipmentCheckPlanTarget.class)
                    .equal("planId", planId)
                    .equal("checkType", plan.getTargetType())
                    .equal("targetId", eqp.getBasicsCode())
                    .count();
            if (count > 0) {
                log.warn("忽略已添加的目标");
            }else{
                EquipmentCheckPlanTarget target = new EquipmentCheckPlanTarget();
                target.setPlanId(planId);
                target.setCheckType(plan.getTargetType());
                target.setTargetId(eqp.getBasicsCode());
                target.setTargetName(eqp.getEqpName());
                target.setTargetCode(eqp.getEqpCode());
                target.setTargetSpec(eqp.getSpecType());
                save(target);
            }

        });

    }

    @Override
    @Transactional
    public void addAreaTarget(EquipmentCheckPlanTarget equipmentCheckPlanTarget) {
        // 查询区域信息
        EquipmentArea area = equipmentAreaService.getById(EquipmentArea.class, equipmentCheckPlanTarget.getTargetId());
        if (area == null) {
            return;
        }
        equipmentCheckPlanTarget.setTargetCode(area.getAreaCode());
        if (area.getPid() != null && !"0".equals(area.getPid())) {
            String parentAllName = equipmentAreaService.getParentAllNameByPid(area.getPid());
            equipmentCheckPlanTarget.setTargetName(parentAllName +"/"+ area.getAreaName());
        } else {
            equipmentCheckPlanTarget.setTargetName(area.getAreaName());
        }
        save(equipmentCheckPlanTarget);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addEqpTargets(EquipmentCheckPlanTarget equipmentCheckPlanTarget) {
        String[] targetIds = equipmentCheckPlanTarget.getTargetId().split(",");
        String[] targetCodes = equipmentCheckPlanTarget.getTargetCode().split(",");
        String[] targetNames = equipmentCheckPlanTarget.getTargetName().split(",");
        String targetSpec = equipmentCheckPlanTarget.getTargetSpec();
        String[] targetSpecs = new String[0];
        if (StringUtils.isNoneEmpty(targetSpec)) {
            targetSpecs = targetSpec.split(",");
        }

        List<EquipmentCheckPlanTarget> targets = new ArrayList<>(targetIds.length);
        for (int i = 0; i < targetIds.length; i++) {
            Long count = JpaUtil.linq(EquipmentCheckPlanTarget.class)
                    .equal("planId", equipmentCheckPlanTarget.getPlanId())
                    .equal("checkType", equipmentCheckPlanTarget.getCheckType())
                    .equal("targetId", targetIds[i])
                    .count();
            if (count > 0) {
                throw new MBootException("重复添加：" + targetCodes[0]);
            }
            EquipmentCheckPlanTarget target = new EquipmentCheckPlanTarget();
            target.setPlanId(equipmentCheckPlanTarget.getPlanId());
            target.setCheckType(equipmentCheckPlanTarget.getCheckType());
            target.setTargetId(targetIds[i]);
            target.setTargetCode(targetCodes[i]);
            target.setTargetName(targetNames[i]);
            target.setTargetSpec(targetSpecs.length > i ? targetSpecs[i] : "");
            targets.add(target);
        }

        save(targets);
    }
}
